//
// Created by root on 19-7-29.
//

#include <unistd.h>
#include <cstdio>
#include "process_utils.h"
#include "log_utils.h"
#include "string_utils.h"

#define TAG "ProcessUtils"

#define BUF_SIZE 1024

std::string get_process_name(pid_t pid) {
    FILE *fp;
    char buffer[BUF_SIZE];
    std::string cmd_str=std::string("cat /proc/");
    cmd_str.append(itoa(pid));
    cmd_str.append("/cmdline");
    const char* cmd=cmd_str.c_str();
    //LOGD("get_process_name cmd:%s\n",cmd);
    fp=popen(cmd, "r");
    fgets(buffer,sizeof(buffer),fp);
    LOGD("process_name:%s\n",buffer);
    return std::string(buffer);
}

bool is_exclude_process(const char *exclude_process) {
    //SystemProperties.set("sys.boot_completed", "1");
    /*char value[PROP_VALUE_MAX];
    int len= __system_property_get("sys.boot_completed",value);
    return strcmp(value,"1")==0;*/
    if(!exclude_process)
        return false;
    std::string proc_name_str = get_process_name(getpid());
    bool ret = proc_name_str.compare(exclude_process) == 0;
    if (ret)
        LOGE("skip %s because it will be started before AuthService\n", exclude_process);
    return ret;
}

#define FIRST_APP_UID 10000
#define LAST_APP_UID 19999
bool is_system_process() {
    int uid = getuid();
    bool user_process = (uid >= FIRST_APP_UID && uid <= LAST_APP_UID);
    return !user_process;
}
